package org.nlp2rdf.navigator.client;

import java.util.ArrayList;
import java.util.List;

import org.nlp2rdf.navigator.client.common.StoredConceptDTO;
import org.nlp2rdf.navigator.client.functionality.ConceptWidget;
import org.nlp2rdf.navigator.client.functionality.TitleWidget;
import org.nlp2rdf.navigator.client.util.ZebraLayouter;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;

public class StoredConceptWidget extends Composite {

	private Application app;

	private List<StoredConceptDTO> storedConcepts = new ArrayList<StoredConceptDTO>();
	
//	private Tree conceptTree;
	
//	private boolean displayAsTree = false;
	
	private VerticalPanel conceptList = new VerticalPanel();
//	private VerticalPanel bodyPane l = new VerticalPanel();
	TitleWidget titleWidget = new TitleWidget("Concepts of other Users", conceptList);

	public StoredConceptWidget(Application application) {
		this.app = application;
		titleWidget.collapse(false);
		
		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.add(titleWidget);
		mainPanel.addStyleName("module-border");
		mainPanel.setWidth("95%");
		mainPanel.addStyleName("greyed");
		mainPanel.add(conceptList);
		conceptList.setWidth("100%");
		
		initWidget(mainPanel);
		update();

//		HorizontalPanel treeOrList = new HorizontalPanel();
//
//		Hyperlink treeClickLabel = new Hyperlink("Tree","stored_as_tree");
//		treeClickLabel.addClickHandler(new ClickHandler() {
//
//			public void onClick(ClickEvent arg0) {
//				
//				
//				displayAsTree = true;
//				draw();
//
//			}
//		});
//		treeOrList.add(treeClickLabel);
//		Hyperlink listClickLabel = new Hyperlink("List","stored_as_list");
//		listClickLabel.addClickHandler(new ClickHandler() {
//
//			public void onClick(ClickEvent arg0) {
//				displayAsTree = false;
//				draw();
//
//			}
//		});
//		treeOrList.add(listClickLabel);
//		subtitlePanel.add(treeOrList);
//		treeOrList.setWidth("40%");
//
//		conceptList = new VerticalPanel();
//		conceptList.setWidth("100%");
//		conceptTree = new Tree();
//		conceptTree.setWidth("100%");
//		conceptTree.setAnimationEnabled(true);
//		ScrollPanel conceptTreePanel = new ScrollPanel();
//		conceptTreePanel.setWidth("100%");
//		conceptTreePanel.add(conceptTree);
//
//		bodyPanel.add(conceptList);
//		bodyPanel.add(conceptTreePanel);
//		mainPanel.add(bodyPanel);
		
		
	}



	private void draw() {
		
		conceptList.setVisible(true);
		conceptList.clear();

		ZebraLayouter z = new ZebraLayouter();
		for (StoredConceptDTO concept : storedConcepts) {
			ConceptWidget w = new ConceptWidget(concept, app);
			w.displayAsRelatedConcept();
			conceptList.add(w);
			z.addWidget(w);
		}
		
//		if (displayAsTree) {
//			conceptTree.setVisible(true);
//			conceptList.setVisible(false);
//			
//			TreeItem root1 = new TreeItem(storedConcepts.get(0).getLabel());
//			
//			TreeItem root1_1 = new TreeItem(storedConcepts.get(1).getLabel()+"1");
//			root1.addItem(root1_1);
//			TreeItem root1_2 = new TreeItem(storedConcepts.get(2).getLabel()+"2");
//			root1.addItem(root1_2);
//			conceptTree.addItem(root1);
//		}
	}

//	private VerticalPanel learningResultToPanel(
//			final LearningResultDTO concept) {
//		VerticalPanel conceptMain = new VerticalPanel();
//		conceptMain.setWidth("100%");
//		
//		if(concept.getLabel()!=null){
//			Label label = new Label(concept.getLabel());
//			label.addStyleName("label");
//		}
//		
//		HorizontalPanel conceptTextPanel = new HorizontalPanel();
//		conceptMain.add(conceptTextPanel);
//		VerticalPanel conceptMetaPanel = new VerticalPanel();
//		conceptTextPanel.setWidth("100%");
//		conceptMetaPanel.setWidth("100px");
//		conceptMetaPanel.addStyleName("padding-left:10px");
//		//XXX TODO 
////		conceptTextPanel.add(new HTML(LearningResultHelper.getHTML(concept)+""));
//		conceptTextPanel.add(conceptMetaPanel);
//		conceptTextPanel.setCellHorizontalAlignment(conceptMetaPanel, HasHorizontalAlignment.ALIGN_RIGHT);
//		
//		// calculate Rating
//		double rating = 0;
//
//		if (concept.getRatings().size() > 0) {
//			for (RatingDTO ratingdto : concept.getRatings()) {
//				rating += ratingdto.getRating();
//			}
//			rating = rating / concept.getRatings().size();
//		}
//
//		if (concept.isStorableConcept()) {
//			// conceptPanel.add(new Rating((int) Math.round(rating),5));
//			Hyperlink load = new Hyperlink("load", "load");
//			load.addClickHandler(new ClickHandler() {
//
//				public void onClick(ClickEvent arg0) {
//					app.displayLearnedConcept(concept);
//
//				}
//			});
//			conceptMetaPanel.add(load);
//		}
//		// conceptPanel.add(new Rating((int) Math.round(rating),5));
//		final Hyperlink export = new Hyperlink("export","export");
//		export.addClickHandler(new ClickHandler() {
//
//			public void onClick(ClickEvent arg0) {
//				final DialogBox dialogue = new DialogBox();
//				VerticalPanel exportPanel = new VerticalPanel();
//				dialogue.add(exportPanel);
//				TextArea area = new TextArea();
//				area.setText(LearningResultHelper.getNoHTML(concept));
//				exportPanel.add(area);
//				dialogue.showRelativeTo(export);
//				Hyperlink ok = new Hyperlink("close","close");
//			      ok.addClickHandler(new ClickHandler() {
//			        public void onClick(ClickEvent event) {
//			         dialogue.hide();
//			        }
//			      });
//			      exportPanel.add(ok);  
//
//			}
//		});
//
//		conceptMetaPanel.add(export);
//		return conceptMain;
//	}

	public void update() {
		app.getWorkerService().getStoredConcepts(
				new AsyncCallback<List<StoredConceptDTO>>() {
					public void onSuccess(List<StoredConceptDTO> dtos) {
						storedConcepts = dtos;
						draw();
					}

					public void onFailure(Throwable arg0) {
						app.reportError(arg0);
					}
				});
	}


}
